import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      name: "home",
      component: HomeView,
    },
    {
      path: "/cate",
      name: "cate",
      component: () => import("../views/CateView.vue"),
    },
    {
      path: "/cart",
      name: "cart",
      component: () => import("../views/CartView.vue"),
      meta:{
        requireLogin:true,
      }
    },
    {
      path: "/my",
      name: "my",
      component: () => import("../views/MyView.vue"),
      meta:{
        requireLogin:true,
      }
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/LoginView.vue"),
    },
    {
      path: "/reg",
      name: "reg",
      component: () => import("../views/RegView.vue"),
    },
    {
      path: "/search",
      name: "search",
      component: () => import("../views/SearchView.vue"),
    },
    {
      path: "/detail",
      name: "detail",
      component: () => import("../views/DetailView.vue"),
    },
    {
      path: "/updatepwd",
      name: "detaupdatepwdil",
      component: () => import("../views/UpdatepwdView.vue"),
    },
  ],
});
router.beforeEach((to,from,next)=>{
  if(to.meta.requireLogin==true){
    let token=localStorage.getItem("token");
    if(token==null){
      alert("请先登录");
      next({path:'/login'})
    }else{
      next();
    }
  }else{
    next();
  }
})

export default router;
